package com.unohacha.qqj.fzdr.ui.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.qpg.superhttp.SuperHttp;
import com.qpg.superhttp.callback.SimpleCallBack;
import com.qpg.widget.common.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.bean.BaseBean;
import com.unohacha.qqj.fzdr.bean.BaseListBean;
import com.unohacha.qqj.fzdr.bean.ImgSearchResult;
import com.unohacha.qqj.fzdr.bean.SearchBean;
import com.unohacha.qqj.fzdr.main.FindDetailActivity2;
import com.unohacha.qqj.fzdr.ui.fragment.FindImgDetialFragment;
import com.unohacha.qqj.fzdr.util.Prefer;
import com.unohacha.qqj.fzdr.util.SpaceItemDecoration;
import com.unohacha.qqj.mylibrary.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 首页搜索
 */
public class SearchActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener{
    private EditText etSearchContent;
    @BindView(R.id.mRefreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.recycleview)
    RecyclerView recycleview;
    @BindView(R.id.common_back)
    ImageView commonBack;
    private String type="1";
    private int a;
    SharedPreferences sharedPreferences;
    private List<SearchBean> list;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    protected void initView() {
        super.initView();

    }

    @Override
    protected void initData() {
        super.initData();
        etSearchContent=findViewById(R.id.et_search_content);
        commonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        sharedPreferences=getSharedPreferences("locationInfo",MODE_PRIVATE);
        WindowManager manager = getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        a = outMetrics.widthPixels - 50;
        GridLayoutManager layoutManage = new GridLayoutManager(this, 2);
        recycleview.setLayoutManager(layoutManage);
        recycleview.addItemDecoration(new SpaceItemDecoration(20));
        etSearchContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.toString().length()==0){
                   return;
                }
                getData(s.toString());
            }
        });
    }
    private class MyAdapter extends BaseQuickAdapter<SearchBean, BaseViewHolder> {

        public MyAdapter(int layoutResId, @Nullable List<SearchBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, SearchBean item) {
            ImageView imageView = (ImageView) helper.getView(R.id.supply_item_type_image);
            imageView.setMinimumWidth(a / 2);
            imageView.setMinimumHeight(a / 2);
            Glide.with(SearchActivity.this)
                    .load(item.getList_pic())
            //        .error(R.mipmap.ic_default_photo)
                    .override(a / 2, a / 2)
                    .into(imageView);

            helper.setText(R.id.supply_item_type_text, item.getTitle());
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        String typeId=list.get(position).getSupply_cate_id();
        String mId=list.get(position).getId();
        Intent intent=new Intent(SearchActivity.this, FindDetailActivity2.class);
        intent.putExtra("typeid",Integer.valueOf(typeId));
        intent.putExtra("id",Integer.valueOf(mId));
        startActivity(intent);
    }
    private void getData(String keyword){
          showLoading();
        String cityId=Prefer.obtainReLocation(SearchActivity.this)+"";
        String token= Prefer.getInstance().getToken();
        SuperHttp.post("api/homerelevant/supplySearch")
                .addParam("token", token)
                .addParam("keyword",keyword)
                .addParam("address_id",cityId)
                .addParam("page","1")
                .request(new SimpleCallBack<BaseBean<BaseListBean<SearchBean>>>() {
                    @Override
                    public void onSuccess(BaseBean<BaseListBean<SearchBean>> data) {
                        hideLoading();
                        list=new ArrayList<>();
                        if(data.getCode()==0){
                            ToastUtil.showCustomToast("暂无数据");
                            return;
                        }
                        list.clear();

                        list.addAll(data.getData().getData());
                        MyAdapter adapter = new MyAdapter(R.layout.activity_supply_demand_grid_two_item, list);
                        adapter.setOnItemClickListener(SearchActivity.this);
                        recycleview.setAdapter(adapter);
                        if (mRefreshLayout.isRefreshing()) {
                            mRefreshLayout.finishRefresh();
                        }


                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                               hideLoading();
                    }
                });
    }
}
